
const config = require("../../utils/config.js");
Page({
  data:{
    item:{},
    isCollection:false,
    isPlay:false,
    discuss:[]
  },
onLoad:function(options){
  var id=options.id;
  console.log(id);
  this.data.currentid=id;
  wx.request({
    url: config.data+'/article/'+id,
    success:(res)=>{
      // console.log(res.data.data);

      this.setData({
        item:res.data.data
      })
    }
  })
  wx.request({
    url: 'http://api.cn/comments/'+id,
    success:(res)=>{
      console.log(res);
      this.setData({
        discuss:res.data.data
      })
    }
  })

  },

collection:function(){
  this.setData({
    isCollection:!this.data.isCollection
  })
  this.show();
},
show:function(){
  if(this.data.isCollection){
    wx.showToast({
      title: '收藏成功',
      icon:'success',
      duration:3000
    })
  }else{
    wx.showToast({
      title: '收藏失败',
      icon: 'success',
      duration: 3000
    })
  }
},
music:function(){
  this.setData({
    isPlay:!this.data.isPlay
  })

  // var currentMovie=common.data[this.data.currentid];
  // console.log(currentMovie);
  const music = wx.getBackgroundAudioManager()
if(this.data.isPlay){
  
  music.title = this.data.item.music.title;
  music.epname = this.data.item.music.epname;
  music.singer = this.data.item.music.singer;
  music.coverImgUrl = this.data.item.music.coverImgUrl;
  // 设置了 src 之后会自动播放
  music.src = this.data.item.music.src;
}else{
  music.pause();
}
},
senddiscuss:function(event){
  var token = wx.getStorageSync('token');
  if (!token){
    wx.showToast({
      title: '请先登录',
    })
    return false;
  }
  // console.log(event);
  var articleid=event.target.dataset.id;
  var discuss=event.detail.value;
  var token=wx.getStorageSync('token');
  wx.request({
    url: 'http://api.cn/comment',
    method:'post',
    header:{
      token: token
    },
    data:{
      content:discuss,
      articleid: articleid
    },
    success:(res)=>{
      // console.log(res);
      if(res.data.status==1){
        wx.showToast({
          title: '发布评论成功',
        })
      }
    }
  })



}

})
